package com.ruoyi.task.dynamicSched;

import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

// 调度任务配置,enable属性为全局开关
@Data
public class ScheduleTaskProperties {

    private Long version;
    private Boolean enable;
    private List<ScheduleTasks> tasks;

    // 调度任务集合,笔者设计的时候采用一个宿主类中每个独立方法都是一个任务实例的模式
    @Data
    public static class ScheduleTasks {
        private Boolean enable;
        // 这里故意叫Class代表Class,避免关键字冲突
        private String taskClass;
        private List<ScheduleTaskMethod> taskMethods;
    }

    // 调度任务方法 - enable为任务开关，没有配置会被ScheduleTaskProperties或者ScheduleTasks中的enable覆盖
    @Data
    public static class ScheduleTaskMethod {

        private Boolean enable;
        private String taskMethod;
        private ScheduleTaskType taskType;
        private String taskDescription;
        // 时区,cron的计算需要用到
        private String timeZone;
        private String cronExpression;
        private String intervalMilliseconds;
        private String initialDelayMilliseconds;
    }

    // 调度任务类型枚举
    @Getter
    @RequiredArgsConstructor
    public enum ScheduleTaskType {

        CRON("CRON"),

        FIXED_DELAY("FIXED_DELAY"),

        FIXED_RATE("FIXED_RATE"),

        ;

        private final String type;

        public static Map<String, ScheduleTaskType> valueMap;

        static {
            valueMap = Arrays.stream(values()).collect(Collectors.toMap(ScheduleTaskType::getType, Function.identity()));
        }

        public static ScheduleTaskType fromType(String taskType) {
            return valueMap.get(taskType);
        }
    }

}